package com.we.wecc.us.mid.impl;

import javax.annotation.Resource;

import com.we.wecc.us.db.mapper.UserAddressMapper;
import com.we.wecc.us.db.mapper.UserCoinAddressMapper;
import com.we.wecc.us.db.mapper.UserTickerLogMapper;
import com.we.wecc.us.db.model.*;
import com.we.wecc.us.mid.model.in.UserAddressIn;
import com.we.wecc.us.mid.model.in.UserCoinAddressIn;
import com.x.share.db.model.Pagination;
import com.x.share.mid.exception.HelperException;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.mid.service.UserService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{

	@Resource
	UserMapper userMapper;

	@Resource
	UserCoinAddressMapper userCoinAddressMapper;

	@Resource
	UserTickerLogMapper userTickerLogMapper;

	@Resource
	UserAddressMapper userAddressMapper;

	@Override
	public User getUserDetail(Long userId) {
		User user = userMapper.selectByPrimaryKey(userId);
		return user;
	}

	@Override
	public UserCoinAddress saveCoinAddress(UserCoinAddressIn in){
		UserCoinAddress ua = in.toUserCoinAddress();
		ua.setCreateTime(new Date());
		if(in.getId() != null){
			userCoinAddressMapper.updateByPrimaryKeySelective(ua);
		}else{
			userCoinAddressMapper.insertSelective(ua);
		}
		return ua;
	}

	@Override
	public Pagination<UserCoinAddress> getUserCoinAddressList(Long userId, Integer pageNo, Integer pageSize) {
		UserCoinAddressExample ex = new UserCoinAddressExample();
		ex.or().andUserIdEqualTo(userId).andDelFlagEqualTo(0);
		ex.setOrderByClause("createTime desc");
		RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
		List<UserCoinAddress> list = userCoinAddressMapper.selectByExampleWithBLOBsWithRowbounds(ex, rb);
		int count = (int)userCoinAddressMapper.countByExample(ex);
		Pagination<UserCoinAddress> page = new Pagination<>(pageNo, pageSize);
		page.setItems(list);
		page.setRecords(count);
		return page;
	}

	@Override
	public Boolean deleteUserCoinAddress(Long userCoinAddressId) {
		Boolean flag = false;
		UserCoinAddress ua = userCoinAddressMapper.selectByPrimaryKey(userCoinAddressId);
		ua.setDelFlag(-1);
		int i = userCoinAddressMapper.updateByPrimaryKeySelective(ua);
		if(i == 1){
			flag = true;
		}
		return flag;
	}

	@Override
	public Map<String,Object> getLastCoinAddress(Long userId) {
		Map<String,Object> ret = new HashMap<>();
		UserTickerLogExample ex = new UserTickerLogExample();
		ex.or().andUserIdEqualTo(userId).andReasonEqualTo("提现到交易所");
		ex.setOrderByClause("createTime desc");
		List<UserTickerLog> list =  userTickerLogMapper.selectByExample(ex);
		if(list.isEmpty()){
			return null;
		}
		String s = list.get(0).getAddress();
		UserCoinAddressExample uadex = new UserCoinAddressExample();
		uadex.or().andAddressEqualTo(s);
		uadex.setOrderByClause("createTime desc");
		List<UserCoinAddress> userAddress = userCoinAddressMapper.selectByExample(uadex);
		if(userAddress.isEmpty()){
			ret.put("tag", "上次使用的地址");
			ret.put("address", s);
		}else{
			ret.put("tag", userAddress.get(0).getTag());
			ret.put("address", s);
		}
		return ret;
	}

	@Override
	public UserAddress saveAddress(UserAddressIn in) {
		UserAddress ua = in.toUserAddress();
		ua.setCreateTime(new Date());
		if(in.getId() != null){
			userAddressMapper.updateByPrimaryKeySelective(ua);
		}else{
			userAddressMapper.insertSelective(ua);
		}
		return ua;
	}

	@Override
	public Pagination<UserAddress> getUserAddressList(Long userId, Integer pageNo, Integer pageSize) {
		UserAddressExample ex = new UserAddressExample();
		ex.or().andUserIdEqualTo(userId).andDelFlagEqualTo(0);
		ex.setOrderByClause("createTime desc");
		RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
		List<UserAddress> list = userAddressMapper.selectByExampleWithRowbounds(ex,rb);
		int count = (int)userAddressMapper.countByExample(ex);
		Pagination<UserAddress> page = new Pagination<>(pageNo, pageSize);
		page.setItems(list);
		page.setRecords(count);
		return page;
	}

	@Override
	public UserAddress getUserAddress(Long id) {
		if(id <= 0){
			throw new HelperException("该用户收货地址不存在！");
		}
		UserAddress userAddress = userAddressMapper.selectByPrimaryKey(id);
		return userAddress;
	}

	@Override
	public Boolean deleteUserAddress(Long userAddressId) {
		Boolean flag = false;
		UserAddress ua = userAddressMapper.selectByPrimaryKey(userAddressId);
		ua.setDelFlag(-1);
		int i = userAddressMapper.updateByPrimaryKeySelective(ua);
		if(i == 1){
			flag = true;
		}
		return flag;
	}
}
